﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Dotnet.Utils.Utility.DrawingUtil
{
    public static class ImageParse
    {
        public static byte[] ParseImage(byte[] data)
        {
            byte[] result;
            using (Stream stream = new MemoryStream(data))
            {
                int num = 240;
                int num2 = 240;
                Image image = ZoomAuto(stream, num, num2, ImageFormat.Jpeg, null, null);
                MemoryStream memoryStream = new MemoryStream();
                image.Save(memoryStream, ImageFormat.Jpeg);
                byte[] array = memoryStream.ToArray();
                memoryStream.Close();
                result = array;
            }
            return result;
        }

        public static void ZoomAuto(Stream fromFile, string savePath, double targetWidth, double targetHeight, ImageFormat imgFormat, string watermarkText, string watermarkImage)
        {
            string directoryName = Path.GetDirectoryName(savePath);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            Image image = Image.FromStream(fromFile, true);
            if (image.Width <= targetWidth && image.Height <= targetHeight)
            {
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image))
                    {
                        Font font = new Font("黑体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (!string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image.Width >= image2.Width && image.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array2 = array;
                                int num = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array2[num] = array3;
                                float[][] array4 = array;
                                int num2 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array4[num2] = array3;
                                float[][] array5 = array;
                                int num3 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array5[num3] = array3;
                                float[][] array6 = array;
                                int num4 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array6[num4] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image.Width - image2.Width, image.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                image.Save(savePath, imgFormat);
            }
            else
            {
                double num5 = image.Width;
                double num6 = image.Height;
                if (image.Width > image.Height || image.Width == image.Height)
                {
                    if (image.Width > targetWidth)
                    {
                        num5 = targetWidth;
                        num6 = image.Height * (targetWidth / image.Width);
                    }
                }
                else if (image.Height > targetHeight)
                {
                    num6 = targetHeight;
                    num5 = image.Width * (targetHeight / image.Height);
                }
                Image image3 = new Bitmap((int)num5, (int)num6);
                Graphics graphics2 = Graphics.FromImage(image3);
                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                graphics2.Clear(Color.White);
                graphics2.DrawImage(image, new Rectangle(0, 0, image3.Width, image3.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image3))
                    {
                        Font font = new Font("宋体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image3.Width >= image2.Width && image3.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image3);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array7 = array;
                                int num7 = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array7[num7] = array3;
                                float[][] array8 = array;
                                int num8 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array8[num8] = array3;
                                float[][] array9 = array;
                                int num9 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array9[num9] = array3;
                                float[][] array10 = array;
                                int num10 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array10[num10] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image3.Width - image2.Width, image3.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                image3.Save(savePath, imgFormat);
                graphics2.Dispose();
                image3.Dispose();
                image.Dispose();
            }
        }

        public static Image ZoomAuto(Stream fromFile, double targetWidth, double targetHeight, ImageFormat imgFormat, string watermarkText, string watermarkImage)
        {
            Image image = Image.FromStream(fromFile, true);
            Image result;
            if (image.Width <= targetWidth && image.Height <= targetHeight)
            {
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image))
                    {
                        Font font = new Font("黑体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (!string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image.Width >= image2.Width && image.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array2 = array;
                                int num = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array2[num] = array3;
                                float[][] array4 = array;
                                int num2 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array4[num2] = array3;
                                float[][] array5 = array;
                                int num3 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array5[num3] = array3;
                                float[][] array6 = array;
                                int num4 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array6[num4] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image.Width - image2.Width, image.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                result = image;
            }
            else
            {
                double num5 = image.Width;
                double num6 = image.Height;
                if (image.Width > image.Height || image.Width == image.Height)
                {
                    if (image.Width > targetWidth)
                    {
                        num5 = targetWidth;
                        num6 = image.Height * (targetWidth / image.Width);
                    }
                }
                else if (image.Height > targetHeight)
                {
                    num6 = targetHeight;
                    num5 = image.Width * (targetHeight / image.Height);
                }
                Image image3 = new Bitmap((int)num5, (int)num6);
                Graphics graphics2 = Graphics.FromImage(image3);
                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                graphics2.Clear(Color.White);
                graphics2.DrawImage(image, new Rectangle(0, 0, image3.Width, image3.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image3))
                    {
                        Font font = new Font("宋体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image3.Width >= image2.Width && image3.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image3);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array7 = array;
                                int num7 = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array7[num7] = array3;
                                float[][] array8 = array;
                                int num8 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array8[num8] = array3;
                                float[][] array9 = array;
                                int num9 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array9[num9] = array3;
                                float[][] array10 = array;
                                int num10 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array10[num10] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image3.Width - image2.Width, image3.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                graphics2.Dispose();
                image.Dispose();
                result = image3;
            }
            return result;
        }
    }
}